package com.evan.aiu.controller;

import com.evan.aiu.entity.Comment;
import com.evan.aiu.result.Result;
import com.evan.aiu.result.ResultFactory;
import com.evan.aiu.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api("评论管理")
public class CommentController {
    @Autowired
    CommentService commentService;

    @ApiOperation("新建评论")
    @PostMapping("api/admin/comment")
    public Result saveComment(@RequestBody @Valid Comment comment) {
        commentService.addOrUpdate(comment);
        return ResultFactory.buildSuccessResult("保存成功");
    }

    @ApiOperation("通过id获取评论")
    @GetMapping("/api/comment/{id}")
    public Result getOneComment(@PathVariable("id") int id) {
        return ResultFactory.buildSuccessResult(commentService.findById(id));
    }

    @ApiOperation("通过aid获取评论")
    @GetMapping("/api/commentByAid/{aid}")
    public Result getComment(@PathVariable("aid") int aid) {
        return ResultFactory.buildSuccessResult(commentService.findByAid(aid));
    }

    @ApiOperation("通过id删除评论")
    @DeleteMapping("/api/admin/comment/{id}")
    public Result deleteArticle(@PathVariable("id") int id) {
        commentService.delete(id);
        return ResultFactory.buildSuccessResult("删除成功");
    }
}
